package com.fzhucc.modules.lesson.service.impl;

import com.fzhucc.exception.BadRequestException;
import com.fzhucc.modules.lesson.domain.LessonFirstChapter;
import com.fzhucc.modules.lesson.domain.LessonSecondChapter;
import com.fzhucc.modules.lesson.domain.dto.LessonSecondChapterDto;
import com.fzhucc.modules.lesson.domain.dto.LessonSecondChapterQueryCriteria;
import com.fzhucc.modules.lesson.repository.LessonFirstChapterRepository;
import com.fzhucc.modules.lesson.repository.LessonSecondChapterRepository;
import com.fzhucc.modules.lesson.service.LessonSecondChapterService;
import com.fzhucc.modules.lesson.service.mapstruct.LessonSecondChapterMapper;
import com.fzhucc.utils.*;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
* @description 服务实现
* @author yyg
* @date 2022-12-08
**/
@Service
@RequiredArgsConstructor
public class LessonSecondChapterServiceImpl implements LessonSecondChapterService {

    private final LessonSecondChapterRepository lessonSecondChapterRepository;
    private final LessonSecondChapterMapper lessonSecondChapterMapper;

    private final LessonFirstChapterRepository lessonFirstChapterRepository;

    @Override
    public Map<String,Object> queryAll(LessonSecondChapterQueryCriteria criteria, Pageable pageable){
        Page<LessonSecondChapter> page = lessonSecondChapterRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(lessonSecondChapterMapper::toDto));
    }

    @Override
    public List<LessonSecondChapterDto> queryAll(LessonSecondChapterQueryCriteria criteria){
        return lessonSecondChapterMapper.toDto(lessonSecondChapterRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public LessonSecondChapterDto findById(Integer id) {
        LessonSecondChapter lessonSecondChapter = lessonSecondChapterRepository.findById(id).orElseGet(LessonSecondChapter::new);
        ValidationUtil.isNull(lessonSecondChapter.getId(),"LessonSecondChapter","id",id);
        return lessonSecondChapterMapper.toDto(lessonSecondChapter);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LessonSecondChapterDto create(LessonSecondChapter resources) {
        Long userId = SecurityUtils.getCurrentUserId();
        if (userId == null) {
            throw new BadRequestException("用户ID未找到");
        }
        Optional<LessonFirstChapter> res = lessonFirstChapterRepository.findById(resources.getFirstChapterId());
        if (!res.isPresent()) {
            throw new BadRequestException("课程一级分类未找到");
        }
        return lessonSecondChapterMapper.toDto(lessonSecondChapterRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(LessonSecondChapter resources) {
        Long userId = SecurityUtils.getCurrentUserId();
        if (userId == null) {
            throw new BadRequestException("用户ID未找到");
        }
        LessonSecondChapter lessonSecondChapter = lessonSecondChapterRepository.findById(resources.getId()).orElseGet(LessonSecondChapter::new);
        ValidationUtil.isNull( lessonSecondChapter.getId(),"LessonSecondChapter","id",resources.getId());
        lessonSecondChapter.copy(resources);
        lessonSecondChapterRepository.save(lessonSecondChapter);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            lessonSecondChapterRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<LessonSecondChapterDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (LessonSecondChapterDto lessonSecondChapter : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("一级列表id", lessonSecondChapter.getFirstChapterId());
            map.put("内容", lessonSecondChapter.getUrl());
            map.put("分类 视频、文档、图片", lessonSecondChapter.getType());
            map.put("名称", lessonSecondChapter.getName());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}